import O1HD, { managers } from "../../../../engines/gameEngine";
import data_control from "../../../../engines/libs/data_control";
import { INormalLevelStruct } from "../../../../Game/Interface";
import levelAvoidEnemy, { EnemyMoveType } from "../base/levelAvoidEnemy";
import levelAvoidPlayer from "../base/levelAvoidPlayer";
import levelAvoidBase from "../levelAvoidBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class level32 extends levelAvoidBase {
    async Init(info: INormalLevelStruct): Promise<void> {
        await super.Init(info)
    }
    setGameStart() {
        super.setGameStart();

        // managers().soundMgr.playSound("level29/sound/startGame", () => { })
        this.wave3();
        // this.wave2();
        // this.wave3();

    }
    waveNode: cc.Node[] = [];
    wave1Node: cc.Node[] = [];
    wave1() {
        this.scheduleOn(()=>{
            managers().soundMgr.playSound("level32/sound/one", () => { })},2)
        let strListTargetY: number = 300;
        let strList: string[] = [
            "呃嘿哈喽，呃嘿呃就是",
            "刚刚在那边看到你",
            "感觉你闺蜜旁边的你",
            "还蛮可爱的"
        ]
        let starPosList: cc.Vec2[] = [
            cc.v2(-450, -300),
            cc.v2(-450, -600),
            cc.v2(0, -600),
            cc.v2(450, -600),
            cc.v2(450, -300)
        ]
        let delayTime = 0;
        let index = 0;
        for (let i = 0; i < strList.length; i++) {

            for (let j = 0; j < strList[i].length; j++) {
                index++
            }
        }
        // 总时间为12秒
        let onceTime = 12 / index;
        index = 0;
        let speed = 30
        for (let i = 0; i < strList.length; i++) {
            let list = strList[i].split("")
            let startPos = cc.v2((-44 * list.length / 2) + 22, strListTargetY - i * 60)
            for (let j = 0; j < list.length; j++) {
                this.scheduleOn(() => {
                    let LabNode = this.newLabNode();
                    this.waveNode.push(LabNode.node)
                    this.wave1Node.push(LabNode.node)
                    LabNode.init(list[j], this.player.getComponent(levelAvoidPlayer), {
                        moveType: EnemyMoveType.four,
                        moveSpeed: speed,
                        startPos: starPosList[index],
                        targetPos: [cc.v2(startPos.x + j * 44, startPos.y)]
                    }, this)
                    LabNode.startAvoid()

                    index++;
                    index = index >= starPosList.length ? 0 : index
                }, delayTime + j * onceTime)
            }

            delayTime += list.length * onceTime
        }
        let starPos2List: cc.Vec2[] = [
            cc.v2(-1000, -500),
            cc.v2(-1000, -1000),
            cc.v2(0, -1000),
            cc.v2(1000, -1000),
            cc.v2(1000, -500)
        ].sort(() => -1)
        // 2
        delayTime = 0
        for (let i = 0; i < strList.length; i++) {
            let list = strList[i].split("")
            let startPos = cc.v2((-44 * list.length / 2) + 22, strListTargetY - i * 60)
            for (let j = 0; j < list.length; j++) {
                this.scheduleOn(() => {
                    let LabNode = this.newLabNode();
                    this.waveNode.push(LabNode.node)
                    this.wave1Node.push(LabNode.node)
                    LabNode.init(list[j], this.player.getComponent(levelAvoidPlayer), {
                        moveType: EnemyMoveType.four,
                        moveSpeed: speed,
                        startPos: starPos2List[index],
                        targetPos: [cc.v2(startPos.x + j * 44, startPos.y)]
                    }, this)
                    LabNode.startAvoid()

                    index++;
                    index = index >= starPosList.length ? 0 : index
                }, delayTime + j * onceTime)
            }

            delayTime += list.length * onceTime
        }
        this.scheduleOn(() => {
            for (let node of this.wave1Node) {
                let enemy = node.getComponent(levelAvoidEnemy)
                enemy.changeMoveData({
                    moveType: EnemyMoveType.two,
                    moveSpeed: 30,
                    startPos: node.getPosition(),
                    moveDirection: cc.v2(0, -1)
                })
            }
        }, delayTime + 2)
        delayTime += 2
        this.scheduleOn(() => {

            for (let node of this.waveNode) node.destroy()
            this.waveNode = []
            this.wave2();
        }, delayTime + 1)
    }
    wave2Node: cc.Node[] = []
    wave2() {}

    
    wave3() {
        // this.node_ma.startAvoid(false);
        let labList: string[] = ["肥", "胖", "丑", "矮", "穷"];
        let posList: cc.Vec2[] = [cc.v2(-280, 280), cc.v2(-280, 0), cc.v2(-280, -280), cc.v2(280, 280), cc.v2(280, 0), cc.v2(280, -280)]
        for (let i = 0; i < labList.length; i++) {

            let LabNode = this.newLabNode();
            this.waveNode.push(LabNode.node)
            LabNode.init(labList[i], this.player.getComponent(levelAvoidPlayer), {
                moveType: 0,
                moveSpeed: 3,
                startPos: posList[i]//cc.v2(data_control.getRandNumberOfArrayTwo([-280, 280]), data_control.getRandNumberOfArrayTwo([-380, 380]))
            }, this,1.25)
            LabNode.startAvoid()
        }
        this.scheduleAny(() => {
            let lab = labList[Math.floor(data_control.getRandNumberOfArrayTwo([0, labList.length]))]
            let LabNode = this.newLabNode();
            this.waveNode.push(LabNode.node)
            LabNode.node.scale = 1.5
            LabNode.init(lab, this.player.getComponent(levelAvoidPlayer), {
                moveType: 0,
                moveSpeed: 3,
                startPos: cc.v2(data_control.getRandNumberOfArrayTwo([-280, 280]), data_control.getRandNumberOfArrayTwo([-380, 380]))
            }, this,1.25)
            LabNode.startAvoid()
        }, 2)
    }

    // wave3() {
    //     this.node_tuo.startAvoid(false);
    //     // managers().soundMgr.playSound("mainSound/duobi/wave3_0")

    //     let labList: string[] = ["矮", "挫", "穷", "丑", "胖"]
    //     cc.tween(this.node_tuo.node)
    //         .to(0.5, { scale: 0 })
    //         .call(() => {
    //             this.node_tuo.node.destroy()
    //             // this.node_tuo.lab.fontSize = 60;
    //             // this.node_tuo.init("尬", this.player.getComponent(levelAvoidPlayer), {
    //             //     moveType: 0,
    //             //     moveSpeed: 3,
    //             //     startPos: cc.v2(0, 0)
    //             // }, this)
    //             // this.node_tuo.startAvoid();
    //         })
    //         .start()
    //     let posList: cc.Vec2[] = [cc.v2(-280, 280), cc.v2(-280, 0), cc.v2(-280, -280), cc.v2(280, 280), cc.v2(280, 0), cc.v2(280, -280)]

    //     let minScale = 0.5;
    //     let maxScale = 6;


    //     for (let i = 0; i < 6; i++) {
    //         let scale = data_control.getRandNumberOfArrayTwo([minScale, maxScale]);

    //         let LabNode = this.newLabNode();
    //         this.waveNode.push(LabNode.node)
    //         LabNode.init(labList[i], this.player.getComponent(levelAvoidPlayer), {
    //             moveType: EnemyMoveType.five,
    //             moveSpeed:3,
    //             startPos: posList[i],//cc.v2(data_control.getRandNumberOfArrayTwo([-280, 280]), data_control.getRandNumberOfArrayTwo([-380, 380]))
    //             rangleV4: new cc.Vec4(-280, 280, -380, 380)
    //         }, this, scale)

    //         LabNode.startAvoid()
    //         let way = 1;
    //         this.scheduleAny(() => {
    //             if (LabNode.scale > maxScale) {
    //                 way = -1;
    //             }
    //             else if (LabNode.scale < minScale) {
    //                 way = 1;
    //             }
    //             LabNode.scale += way * 0.06;
    //         })
    //     }
    //     // this.scheduleAny(() => {
    //     //     let lab = "尬";
    //     //     let LabNode = this.newLabNode();
    //     //     this.waveNode.push(LabNode.node)
    //     //     LabNode.node.scale = 1.5
    //     //     LabNode.init(lab, this.player.getComponent(levelAvoidPlayer), {
    //     //         moveType: 0,
    //     //         moveSpeed: 3,
    //     //         startPos: cc.v2((Math.random() > 0.5 ? 1 : -1) *data_control.getRandNumberOfArrayTwo([100, 280]), (Math.random() > 0.5 ? 1 : -1) * data_control.getRandNumberOfArrayTwo([100, 380]))
    //     //     }, this, 1.25)
    //     //     LabNode.startAvoid()
    //     // }, 1)
    // }

    Lose(time?: number) {
        this.unscheduleAllCallbacks()
        let pos = this.player
        let jumpTime = cc.v3(100, -1000).sub(this.player.position).mag() / 800
        // managers().soundMgr.playSound('mainSound/duobi/death').then();
        cc.tween(this.player)
            .parallel(
                cc.tween(this.player).bezierTo(jumpTime, this.player.getPosition(), cc.v2(this.player.x + 200, this.player.y + 500), cc.v2(this.player.x + 400, this.player.y - 1000)),
                cc.tween(this.player).to(4, { angle: -3000 })
            )
            .start()
        super.Lose(jumpTime)

    }
    Win(time?: number, jyIndex?: number, userStr?: string): void {
        this.unscheduleAllCallbacks()
        let endScale = this.player.scale
        managers().soundMgr.playSound("fontChase/win").then();
        cc.tween(this.player)
            .to(0.2, { scale: 0.6 })
            .call(() => {
                this.player.getChildByName("spwin").active = true;
            })
            .delay(2)
            .call(() => {
                let bo = this.player.getChildByName("bo")
                bo.active = true;

                cc.tween(bo)
                    .to(1, { scale: 5 })
                    .call(() => {
                        bo.active = false;
                    })
                    .start()
            })
            .to(0.2, { scale: 1 })
            .call(() => {
                for (let node of this.waveNode) {
                    cc.tween(node)
                        .to(0.1, { scale: 1.1, opacity: 255 })
                        .to(0.1, { scale: 1, opacity: 100 })
                        .to(0.1, { scale: 1.1, opacity: 255 })
                        .to(0.1, { scale: 1, opacity: 100 })
                        .to(0.3, { scaleX: 1.2, scaleY: 0.8, opacity: 0 })
                        .call(() => {
                            node.destroy()
                        })
                        .start()
                }
                super.Win(3)
            })
            .start()

    }
}